package com.bst.web.etl;

import com.bst.base.domain.BaseDb;
import com.bst.base.domain.BaseTempItem;
import com.bst.base.service.IBaseDbService;
import com.bst.common.constant.Constants;
import com.bst.common.core.domain.AjaxResult;
import com.bst.common.exception.base.BaseException;
import com.bst.etl.datax.tool.query.BaseQueryTool;
import com.bst.etl.datax.tool.query.QueryToolFactory;
import com.bst.etl.service.ITableMetaQueryService;
import com.bst.etl.vo.OdsTableCreSqlVO;
import com.bst.etl.vo.TableFieldVO;
import com.bst.etl.vo.TableForHosAndOdsParamVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/etl/meta/query")
@Api(tags="表元数据处理")
public class TableMetaQueryController {

    @Autowired
    ITableMetaQueryService tableMetaQueryService;
    @Autowired
    IBaseDbService baseDbService;

    /**
     * 查询基础抽取列表
     */
    @PostMapping("/queryFlds")
    @ResponseBody
    public AjaxResult dateFlds(@RequestBody OdsTableCreSqlVO paramVO)
    {
        List<TableFieldVO> flds = null;
        try {
            BaseDb db = baseDbService.selectBaseDbById(paramVO.getIdDb());
            paramVO.setDb(db);
            if(StringUtils.isNotEmpty(paramVO.getQrySql())) {
                BaseQueryTool queryTool = QueryToolFactory.getByDbType(db);
                flds = queryTool.getColumnsByQuerySql(paramVO.getQrySql(),db.getSdDbtp());
            } else {
                flds = tableMetaQueryService.findAllFldsByTableName(paramVO.getTableName(),paramVO.getIdDb()+"");
            }
        } catch (SQLException throwables) {
            return AjaxResult.error(throwables.getMessage());
        }
        return AjaxResult.success(flds);
    }

    @GetMapping("/creatSQl")
    @ResponseBody
    public AjaxResult queryCreatSQl(String tableName,Integer idDb) throws SQLException {
        String sql = tableMetaQueryService.queryTableCreSql(tableName,idDb);
        return AjaxResult.success(sql);
    }

    @PostMapping("/tableForHosAndOdsFlds")
    @ResponseBody
    public AjaxResult tableForHosAndOdsFlds(@RequestBody TableForHosAndOdsParamVO paramVO) {
        List<String> publicFlds = new ArrayList<>();
        try {
            List<TableFieldVO> fldsSource = paramVO.getSourFields();
            List<TableFieldVO> fldsTar = tableMetaQueryService.findAllFldsByTableName(paramVO.getTableTar(), "dw");
            List<String> tarFldNames = fldsTar.stream().map(TableFieldVO::getName).collect(Collectors.toList());
            tarFldNames = tarFldNames.stream().map(str->str.toUpperCase()).collect(Collectors.toList());
            for (TableFieldVO fld : fldsSource) {
                if(tarFldNames.contains(fld.getName().toUpperCase())) {
                    publicFlds.add(fld.getName().toUpperCase());
                }
            }
        }catch (Exception e) {}
        return AjaxResult.success(publicFlds);
    }

    @PostMapping("/tableCreSql")
    @ResponseBody
    public AjaxResult tableCreSql(@RequestBody OdsTableCreSqlVO paramVO) throws BaseException {
        String sql = tableMetaQueryService.queryOdsTableCreSql(paramVO);
        return AjaxResult.success(sql);
    }

    /**
     * 根据数据源id和sql语句获取所有字段
     *
     * @param idDb 数据源id
     * @param querySql     表名
     * @return
     */
    @GetMapping("/getColumnsByQuerySql")
    @ApiOperation("根据数据源id和sql语句获取所有字段")
    @ResponseBody
    public AjaxResult getColumnsByQuerySql(Integer idDb, String querySql) throws SQLException {
        BaseDb db = baseDbService.selectBaseDbById(idDb);
        BaseQueryTool queryTool = QueryToolFactory.getByDbType(db);
        return AjaxResult.success(queryTool.getColumnsByQuerySql(querySql,db.getSdDbtp()));
    }

}
